//
//  ZQDropdownMenu.m
//  柳州微博
//
//  Created by Apple_Lzzy40 on 16/9/23.
//  Copyright © 2016年 Apple_Lzzy40. All rights reserved.
//

#import "ZQDropdownMenu.h"
@interface ZQDropdownMenu()
@property (nonatomic ,weak)UIImageView * containerView;
@end

@implementation ZQDropdownMenu

-(UIImageView *)containerView
{

    if (_containerView==NULL) {
        UIImageView *container=[[UIImageView alloc]init];
        container.image=[UIImage imageNamed:@"popover_background"];
        container.width=200;
        container.height=200;
//        container.y=130;
        //添加到Cover
        [self addSubview:container];
        
        //打开用户交互功能
        container.userInteractionEnabled=YES;
        
        _containerView = container;
    }
    return _containerView;
}
//遮盖初始化方法
-(instancetype)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor clearColor];
    }
    return self;
}

+(instancetype)dropdownMenu
{
    return [[self alloc]init ];
}

-(void)showAtView:(UIView *)form
{
    //获取屏幕主的窗口
        UIWindow *window=[UIApplication sharedApplication].keyWindow;
    
   //2.把自己添加到主窗口
    [window addSubview:self];
    //3.设置遮盖的尺寸
    self.frame=window.bounds;
   //4.设置灰色图片的位置
    self.containerView.x=30+20;
    self.containerView.y=50;
    
    //转换From坐标
    CGRect newFrame=[form convertRect:form.bounds toView:window];
    //获取中心点的X
    self.containerView.centerX=CGRectGetMidX(newFrame);
    //获取From的最大Y坐标
    self.containerView.y=CGRectGetMaxY(newFrame);
    
    ((UIButton *)form).selected = YES;
    
}

-(void)setContent:(UIView *)content
{
    _content=content;
    //设置内容坐标（位置）
    content.x=10;
    content.y=10+30;
    //内容宽度
    content.width=self.containerView.width-10*2;
    //灰色容器的高度
    self.containerView.height=CGRectGetMaxX(content.frame)+25;
    //添加内容到灰色容器中
    
    [self.containerView addSubview:content];
    //[_containerView addSubview:content]; 
}

-(void)setContentController:(UIViewController *)contentController
{

    _contentController=contentController;
    self.content=contentController.view;
}
/**
 *  销毁
 */
-(void)dismiss
{
    [self removeFromSuperview];
    //3.调用代理方法
    //3.调用协议方法
    if ([self.delegate respondsToSelector:@selector(dropdownMenu:)]) {
        [self.delegate dropdownMenu:self];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{

//    NSLog(@"touchesBegan");
    [self dismiss];
}


@end
